//
//  MHomeViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/10/28.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MHomeViewController.h"
#import "MInfomationViewController.h"
#import "MDDataBaseUtil.h"
#import "AppDelegate.h"
#import "MyVipCardViewController.h"
#import "MFriendViewController.h"
#import "MEnjoyTicketViewController.h"
@interface MHomeViewController () <UIAlertViewDelegate>{

    MInfomationViewController *informationVC;
    MLoginViewController *loginVC;
    MyVipCardViewController *myVipCardVC;
    MFriendViewController *friendVIPVC;
    MEnjoyTicketViewController *enjoyTicketVC;
    NSInteger index;
}
- (IBAction)GoNextVCAction:(UIButton *)sender;
@property (nonatomic,strong) UITapGestureRecognizer *dismissMoreViewRecognizer;
//- (IBAction)GoInfomationVCAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@end

@implementation MHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.navigationController.navigationBar setHidden:YES];
    //添加手势
    self.dismissMoreViewRecognizer.enabled=NO;
    self.dismissMoreViewRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissMoreView:)];
    [self.view addGestureRecognizer: self.dismissMoreViewRecognizer];
    
    //子视图的相关设置
    self.mapViewController=[[MMapViewController alloc]init];
    self.listViewController=[[MListViewController alloc]init];
    
    self.currentViewController = [[UIViewController alloc]init];
    [self addChildViewController:self.mapViewController];
    [self addChildViewController:self.listViewController];
    
    self.mapViewController.view.frame=CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-70);
    self.listViewController.view.frame=CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-70);
    
    [mainView addSubview:self.mapViewController.view];
    [mapButton setSelected:YES];
    self.currentViewController=self.mapViewController;
    
    [mainView addSubview:self.menuView];
    self.menuView.hidden=YES;
    self.titleLabel.text=@"全部";
    loginVC = [[MLoginViewController alloc]init];
    
    informationVC = [[MInfomationViewController alloc]init];
    myVipCardVC = [[MyVipCardViewController alloc]init];
    friendVIPVC = [[MFriendViewController alloc]init];
    enjoyTicketVC = [[MEnjoyTicketViewController alloc]init];
}
- (void)viewWillAppear:(BOOL)animated {
    
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    index = app.isLoginIndex;
    if (index) {// 1 为登陆
        [self.loginBtn setHidden:YES];
        CGRect orginFrame = self.menuView.frame;
        orginFrame.size.height = 210;
        self.menuView.frame = orginFrame;
    }
}
- (void)viewWillDisappear:(BOOL)animated {

    self.menuView.hidden=YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//菜单按钮的事件
-(IBAction)showMenuView:(id)sender{
    [mainView addSubview:self.menuView];
    if (self.menuView.hidden==YES) {
        self.menuView.hidden=NO;
        [UIView animateWithDuration:0.1 animations:^{} completion:^(BOOL finished) {
            self.dismissMoreViewRecognizer.enabled=YES;
            
        }];

    }else{
        self.menuView.hidden=YES;
    }
}
//手势事件
-(void)dismissMoreView:(id)sender{
    self.menuView.hidden=YES;
    self.dismissMoreViewRecognizer.enabled=NO;
}
-(void)showChildView:(UIViewController *)target{
    if (self.currentViewController==target) {
        return;
    }
    UIViewController *oldViewController=self.currentViewController;
    [self transitionFromViewController:self.currentViewController toViewController:target duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{} completion:^(BOOL finished){
        if (finished) {
            self.currentViewController=target;
        }else{
            self.currentViewController=oldViewController;
        }
    }];

}
-(IBAction)showMapView:(id)sender{
    [self showChildView:self.mapViewController];
    [mapButton setSelected:YES];
    [listButton setSelected:NO];
}
-(IBAction)showListView:(id)sender{
    [self showChildView:self.listViewController];
    [mapButton setSelected:NO];
    [listButton setSelected:YES];
}
-(IBAction)loginButton:(id)sender{
    [self presentViewController:loginVC animated:YES completion:nil];

}
- (void)clickListCellMapButtonAction:(UIButton *)sender {
    self.mapViewController.selectedTitle = [sender titleForState:UIControlStateSelected];
    [self showMapView:sender];
}
- (void)clickMapButtonAction:(UIButton *)sender {

    self.mapViewController.selectedTitle = [sender titleForState:UIControlStateSelected];
    [self showMapView:sender];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//- (IBAction)GoInfomationVCAction:(UIButton *)sender {
//    
//    informationVC = [[MInfomationViewController alloc]init];
//    
//    informationVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//    [self presentViewController:informationVC animated:YES completion:nil];
//}
- (IBAction)GoNextVCAction:(UIButton *)sender {
    
    switch (sender.tag) {
        case 100:// 扫描二维码
            
            break;
        case 200:// 我的资料
            
            [self GoNextViewController:informationVC buttonTag:sender.tag];
            break;
        case 300:// 我的VIP卡
            
            [self GoNextViewController:myVipCardVC buttonTag:sender.tag];

            break;
        case 400:// 朋友VIP卡
            [self GoNextViewController:friendVIPVC buttonTag:sender.tag];
            break;
        case 500:// 商家代金券
            break;
        case 600:// 朋友代金券
            [self GoNextViewController:enjoyTicketVC buttonTag:sender.tag];
            break;
        default:
            break;
    }
}
- (void)GoNextViewController:(UIViewController *)viewC  buttonTag:(NSInteger)tag{

    if (index) {// 是登录状态
        viewC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:viewC animated:YES completion:nil];
    }
    else {
    
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请先登录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alert setTag:tag];
        [alert show];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1) {
        [self presentViewController:loginVC animated:YES completion:nil];
    }
    
}
@end
